/*
 * Copyright (c) 2008 Nanometrics Inc.
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package ca.nanometrics.gflot.client;

import ca.nanometrics.gflot.client.options.AbstractSeriesOptions;

/**
 * @author Alexander De Leon
 */
public class SeriesHandler {

	private final Series m_series;
	private SeriesData m_data;

	public SeriesHandler(Series series, SeriesData data) {
		m_series = series;
		m_data = data;
	}

	public void add(DataPoint datapoint) {
		m_data.add(datapoint);
	}

	public void clear() {
		m_data.clear();
	}

	public void setOptions(SeriesType type, AbstractSeriesOptions<?> options) {
		m_series.setSeriesOptions(type, options);
	}

	public void setVisible(boolean visisble) {
		if (visisble) {
			m_series.setData(m_data);
		} else {
			m_series.setData(null);
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof SeriesHandler) {
			return m_series.equals(((SeriesHandler) obj).m_series);
		}
		return false;
	}

	public int hashCode() {
		return m_series.hashCode();
	}

	public Series getSeries() {
		return m_series;
	}

    /* ------------- for internal use */
	SeriesData getData() {
		return m_data;
	}

	void setData(SeriesData newData) {
		m_data = newData;
		// set the data if the series is visible
		if (m_series.getData() != null) {
			m_series.setData(m_data);
		}
	}

}
